package cn.itcast.controller;

import cn.itcast.service.SearchService;
import com.moblie.constant.MessageConstant;
import com.moblie.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(allowCredentials="true",allowedHeaders="*")
public class SearchController {
    @Autowired
    private SearchService searchService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/search")
    public Result search(String search, String phone){
        try{
            redisTemplate.boundListOps(phone+"_searchhistory").leftPush(phone);
            List<Map> map=searchService.search(search);
            return new Result(true, MessageConstant.SEARCH_SUCESS,map);
        }catch (Exception e){
            return new Result(false, MessageConstant.SEARCH_FAIL);
        }
    }
}
